package com.example.springboot.controller.pagehelperController;

import com.example.springboot.bean.Employee;
import com.example.springboot.config.mysql.UseDataSource;
import com.example.springboot.entity.PageParam;
import com.example.springboot.entity.PagedList;
import com.example.springboot.mapper.EmployeeMapper;
import com.example.springboot.utils.PageUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author:ChenZhangKun
 * @Date: 2022/4/17 20:09
 */
@RestController
public class PageHelperController {
    @Autowired
    private EmployeeMapper employeeMapper;

    @PostMapping("/getPageList")
    @UseDataSource(value = "")
    public PagedList<Employee> getPageList(@RequestBody PageParam<Employee> pageParam) {
        PagedList<Employee> employeePagedList = PageUtils.exportPagedList(pageParam);
        return this.queryPageList(employeePagedList, pageParam.getParam());
    }

    public PagedList<Employee> queryPageList(PagedList<Employee> page, Employee employee) {
        PageInfo<Employee> pageInfo = PageHelper.startPage(page).doSelectPageInfo(() ->employeeMapper.selectByParam(employee));
        //转换结果
        return PageUtils.toPageList(pageInfo);
    }
}
